/**
 * @class tuan.controller.KhachHangController
 * @extends extendsClass
 * Description
 */
 Ext.define('tuan.controller.KhachHangController', {
    extend: 'Ext.app.Controller',
    refs : [
        { ref : "actionGrid", selector: "khach_hang_view grid#idgrid"},
        { ref : "actionPanel", selector : "khach_hang_view form#detail_panel_account"},
        {ref : 'formAdd', selector : 'addnewcustomer-window form'},
        {ref : 'gridBank', selector : 'khachhang-bank-xtype'}
    ],
    stores : ["CustomerStore","BankStore"],
    models : ["CustomerModel","BankModel"],
    views : ["khachhang.KhachHangGrid","khachhang.KhachHangPanel","khachhang.KhachHangView","khachhang.AddNewCustomer","khachhang.KhachHangBankGrid"],
    init : function(){
        this.control({
            "khach_hang_view" : {
                beforerender : this.loadAccounts
            },
            "khach_hang_view homepanelgrid" : {
                itemclick  : this.loadDetailAccount
            },
            "khach_hang_view #btnUpdate" : {
                click : this.updateModelAccount
            },
            "khach_hang_view grid menuitem[itemId=btn-add-customer]" : {
            // "#btn-add-customer" : {
                click : this.openWindowAddCustomer
            },
            "button[itemId=btn_add_customer]" : {
                click : this.submitAddCustomer,
            }
        });
    },
    loadAccounts : function(){
        var g = this.getActionGrid();

        var accountStore = Ext.getStore("CustomerStore");
        accountStore.load();

        paging = Ext.getCmp("paging_grid_customer_master");


        // accountStore.on("load", function (accounts) {
        //     g.reconfigure(accounts);
            
        // });
        g.reconfigure(accountStore);
        paging.bindStore(accountStore);
    },
    loadDetailAccount : function(src,record){
        id = record.getId();
        this.getActionPanel().loadRecord(record);

        console.log('load bank')
        //get bank account
        bankStore = Ext.getStore("BankStore");
        bankStore.getProxy().extraParams = {filter : '[{"property" : "accid","value" :"'+id+'","type" : "string","comparison" : "eq"}]'};
        bankStore.load();
        gridBank = this.getGridBank();
        gridBank.reconfigure(bankStore);
    },
    updateModelAccount : function(src){
        if(src.up("form").isValid()){
            form = src.up("form").getForm().getValues();
            // console.log(form)
            customerStore = Ext.getStore('CustomerStore');
            customerStore.each(function(c){
                id = c.get('id');
                if(id === form.id){
                    console.log('update '+id);
                    c.set('address',form.address);
                    c.set('billing_address',form.billing_address);
                    c.set('phone',form.phone);
                    c.set('fullname',form.fullname);
                    c.set('note',form.note);
                    c.getChanges();
                    customerStore.save();
                }
            })
        }   
    },
    openWindowAddCustomer : function(){
        windowAdd = Ext.create('tuan.view.khachhang.AddNewCustomer');
        windowAdd.show();
    },
    submitAddCustomer : function(src){
        if(src.up("form").isValid()){
             form = src.up("form").getValues();
             
             //Send json data to controller
             var writer = Ext.create('Ext.data.writer.Json');
             cs = Ext.create('tuan.model.CustomerModel',form);

             Ext.Ajax.request({
                url: tuan.config.Runtime.linkUrl+'Khachhang/createCustomer',
                headers : {
                    "X-ASCCPE-USERNAME" :'quanly_main',
                    "X-ASCCPE-PASSWORD" : 'giongnhucu'
                },
                method: 'POST',
                params: {
                    action: 'action_create_customer',
                    username : 'truongataunvn'
                },
                jsonData: writer.getRecordData(cs),
                success: function() {
                    Ext.MessageBox.alert('Status', 'Thêm mới thành công');
                    customerStores = Ext.getStore('CustomerStore');
                    customerStores.load();
                    windowAdd = Ext.ComponentQuery.query('#addnewcustomer_id')[0];
                    windowAdd.close();
                },
                failure: function() {
                     Ext.MessageBox.alert('Status', 'Fail, something wrong...try again');
                }
            });
        }
    }
});